const app = getApp()
var template = require('../../template/template.js');
const util = require('../../utils/util');
var typeArrId = {};
var placeArrId = {};
var violationArrId = {};
var type_id;
var placeCur;
var violationCur;
var adds = {};
var insert_id;
var is_submit = false;
var trd_session;
var num;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeArr: ['请选择'],
    placeArr: ['请选择'],
    violationArr: ['请选择'],
    typeIndex: 0,
    placeIndex: 0,
    violationIndex: 0,
    pics: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var tabnum = app.globalData.tabnum;
    console.log(tabnum);
    template.tabbar("tabBar", 2, this, tabnum);//0表示第一个tabbar
    wx.getStorage({
      key: 'trd_session',
      success: function (res) {
        console.log(res.data);
        trd_session = res.data;
      },
    });
    wx.request({
      url: 'https://xcx.chenjiudenanren.com/inter/Index/getTypeArr',
      data: '',
      method: 'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //console.log(res.data);
        typeArrId = res.data.ids,
          that.setData({
            typeArr: res.data.names,
          });
        that.getPlaceList();
      }
    });

  },

  getPlaceList: function () {
    var that = this;
    wx.request({
      url: 'https://xcx.chenjiudenanren.com/inter/index/getPlaceArr',
      data: { type_id: typeArrId[0] },
      method: 'get',
      header: {// 设置请求的 header  
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {

        that.setData({
          placeArr: res.data.names
        });
        placeArrId = res.data.ids;
        placeCur = placeArrId[0];
      }
    });

    wx.request({
      url: 'https://xcx.chenjiudenanren.com/inter/index/getViolationType',
      data: { type_id: typeArrId[0] },
      method: 'get',
      header: {// 设置请求的 header  
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data);
        that.setData({
          violationArr: res.data.names
        });
        violationArrId = res.data.ids;
        violationCur = violationArrId[0];
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  addPic: function () {
    var that = this;
    if (this.data.pics.length < 9) {
      wx.chooseImage({
        count: 9, // 最多可以选择的图片张数，默认9
        //sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
        success: function (res) {
          var imgsrc = that.data.pics.concat(res.tempFilePaths);
          that.setData({
            pics: imgsrc
          });
        },
      })
    } else {
      wx.showToast({
        title: '最多上传九张图片',
        icon: 'loading',
        duration: 1000
      });
    }
  },

  // addVideo
  addVideo: function () {
    var that = this
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      success: function (res) {
        that.setData({
          video: res.tempFilePath,
          size: (res.size / (1024 * 1024)).toFixed(2)
        })
      }
    })
  },
  formSubmit: function (e) {
    if (is_submit) return;
    is_submit = true;
    var id = e.target.id;
    adds = e.detail.value;
    adds.place_id = placeCur;
    adds.repore_id = violationCur;
    adds.trd_session = trd_session;
    this.uploadimg();

  },
  uploadimg: function () {//这里触发图片上传的方法
    var that = this;
    if (this.data.pics.length == 0) {
      wx.showToast({
        title: '请选择至少一张图片',
        duration: 1000
      });
      is_submit = false;
      return;
    }
    if (that.data.size > 1024 * 1024 * 5) {
      wx.showModal({
        title: '视频上传',
        content: '很抱歉，视频最大允许2M，当前为' + (that.data.size / (1024 * 1024)).toFixed(5) + 'M'
      })
      return false;
    }
     wx.showLoading({ title: '提交中' });
        
    wx.request({
      url: 'https://xcx.chenjiudenanren.com/inter/Index/reportInterface',
      data: adds,
      method: 'POST',
      header: {// 设置请求的 header  
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        insert_id = res.data;
        that.upVideo();
        that.upPics();
       
      }
    })
  },
  upVideo: function () {
    var that = this;
    wx.uploadFile({
      url: 'https://xcx.chenjiudenanren.com/inter/Sy/upload',
      method: 'POST',
      filePath: that.data.video,
      header: {        'content-type': 'multipart/form-data'      },
      name: 'video',
      formData: { id: insert_id },
      success: function (r) {
       // console.log(r)
       // wx.hideLoading();      
      },
      fail: function (r) {

      }
    })
  },
  upPics: function () {
    var that = this;
    for (var i = 0; i < this.data.pics.length; i++) {
      num = i + 1;
      wx.uploadFile({
        url: 'https://xcx.chenjiudenanren.com/inter/Index/reportImgUpload',
        filePath: that.data.pics[i],
        name: 'pic',
        formData: { id: insert_id },
        success: function (res) {
          wx.hideLoading();  
          if (res) {
            wx.showToast({
              title: '提交成功',
              duration: 1000
            });
          }
          if (num == res.data) {
            setTimeout(function () {
              wx.redirectTo({
                url: '../complain/complain'
              })
            }, 2000)
          }
          is_submit = false;
        },
        fail: function (r) {
          wx.hideLoading();  
          wx.showToast({
            title: '提交失败',
            duration: 1000
          });
        }
      })
    }
  },
  onShareAppMessage: function () {

  },

  bindTypeChange: function (e) {
    var that = this;
    this.setData({
      typeIndex: e.detail.value
    });
    type_id = typeArrId[e.detail.value];
    wx.request({
      url: 'https://xcx.chenjiudenanren.com/inter/index/getPlaceArr',
      data: { type_id: type_id },
      method: 'get',
      header: {// 设置请求的 header  
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //console.log(res.data);
        that.setData({
          placeArr: res.data.names,
        });
        placeArrId = res.data.ids;
      }
    });
    wx.request({
      url: 'https://xcx.chenjiudenanren.com/inter/index/getViolationType',
      data: { type_id: type_id },
      method: 'get',
      header: {// 设置请求的 header  
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          violationArr: res.data.names,
        });
        violationArrId = res.data.ids;
      }
    });
  },

  bindPlaceChange: function (e) {
    this.setData({
      placeIndex: e.detail.value,
    });
    console.log(e.detail.value);
    console.log(placeArrId);
    placeCur = placeArrId[e.detail.value];
  },

  bindReporeChange: function (e) {
    this.setData({
      violationIndex: e.detail.value,
    });
    console.log(e.detail.value);
    console.log(violationArrId);
    violationCur = violationArrId[e.detail.value];
  },
})